<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of shippingcom
 *
 * @author hassanali
 */
class packages extends admin_controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('admin/packages_model', 'packages');
        $this->load->model('admin/packages_details_model', 'pdetails');
    }

    public function index() {
        $data['result'] = $this->packages->filter();
        $data['pagination'] = $this->admin_pagination('index', $this->packages->filter(true), 10);
        $this->themes->render('packages/index', $data);
    }

    public function view($id = FALSE) {

        if ($id) {
            $falid_id = $this->packages->is_found(array('id' => $id), array(), 'row', __CLASS__);
            $data['details'] = $this->pdetails->get();
            $data['result'] = $falid_id;
            $this->themes->render('packages/view', $data);
        } else {
            show_404();
        }
    }

    public function edit($id = false) {
        if ($id) {
            $pass['language'] = $languages = get_languages();
            $id_id = $this->packages->is_found(array('id' => $id), array(), 'row', __CLASS__);
            $pass['package'] = $id_id;
            $pass['packagelanguage'] = $this->packages->getlanguage($id);
            $pass['details'] = $this->pdetails->get();
            if ($this->input->post()) {
                $this->session->set_userdata('post', $this->input->post());

                $this->form_validation->set_rules('code', lang('label_name'), 'trim|callback_title_required|callback_values_required');
                $this->form_validation->set_rules('price', lang('label_price'), 'trim|required|numeric');
                $this->form_validation->set_rules('manage', lang('label_manage'), 'trim|required|numeric');
                if ($this->form_validation->run()) {

                    $data['values'] = serialize($this->input->post('details'));
                    $data['price'] = $this->input->post('price');
                    $data['manage'] = $this->input->post('manage');
                    $data['p_id'] = $id;
                    foreach ($languages as $la) {
                        if ($this->input->post("title_$la->code")) {
                            $data["title_$la->code"] = $this->input->post("title_$la->code");
                        }
                    }
                    if ($this->packages->save($data, array('id' => $id), TRUE)) {
                        $this->session->unset_userdata('post');
                        $this->themes->set_message(array('url' => __CLASS__, 'type' => 'success', 'message' => 'message_edit_success'));
                    } else {
                        get_save_error();
                    }
                } else {
                    get_validation_errors();
                }
            } else {
                $this->themes->render('packages/edit', $pass);
            }
        } else {
            show_404();
        }
    }

    public function title_required() {
        $check = FALSE;
        $lang = get_languages();
        foreach ($lang as $l) {
            if ($this->input->post('title_' . $l->code) && $l->default == 'YES') {
                $check = TRUE;
                break;
            }
        }
        if ($check) {
            return TRUE;
        } else {
            $this->form_validation->set_message('title_required', lang('message_title_required'));
            return FALSE;
        }
    }

    public function values_required() {
        $check = TRUE;
        $details = $this->pdetails->get();
        $detpost = $this->input->post('details');
        foreach ($details as $l) {
            if (!$detpost[$l->name] && $detpost[$l->name] != '0') {
                $check = FALSE;
                break;
            }
        }
        if ($check) {
            return TRUE;
        } else {
            $this->form_validation->set_message('values_required', lang('message_values_required'));
            return FALSE;
        }
    }

    public function add() {
        $data['language'] = $languages = get_languages();
        $data['details'] = $this->pdetails->get();
        if ($this->input->post()) {
            $this->session->set_userdata('post', $this->input->post());

            $this->form_validation->set_rules('code', lang('label_name'), 'trim|callback_title_required|callback_values_required');
            $this->form_validation->set_rules('price', lang('label_price'), 'trim|required|numeric');
            $this->form_validation->set_rules('manage', lang('label_manage_price'), 'trim|required|numeric');
            if ($this->form_validation->run()) {

                $data['values'] = serialize($this->input->post('details'));
                $data['price'] = $this->input->post('price');
                $data['manage'] = $this->input->post('manage');
                foreach ($languages as $la) {
                    if ($this->input->post("title_$la->code")) {
                        $data["title_$la->code"] = $this->input->post("title_$la->code");
                    }
                }
                if ($this->packages->save($data)) {
                    $this->session->unset_userdata('post');
                    $this->themes->set_message(array('url' => __CLASS__, 'type' => 'success', 'message' => 'message_add_success'));
                } else {
                    get_save_error();
                }
            } else {
                get_validation_errors();
            }
        } else {
            $this->themes->render('packages/add', $data);
        }
    }

    public function all() {
        $id = $this->input->post('id');
        $doaction = $this->input->post('doaction');
        // valid if no item select 
        $this->empty_id_actionall($id);
        $this->empty_id_actionall($doaction, 'message_select_action_from_dropdown');

        switch ($doaction) {
            case 'delete': {
                    $this->packages->delete(array('id' => $id));
                    $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_delete_success'));
                }
                break;
            case 'no_trash': {
                    $data['trash'] = 'NO';
                    $this->packages->save($data, array('id' => $id), TRUE);
                    $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;
            case 'delete_final': {
                    $this->packages->delete(array('id' => $id), FALSE);
                    $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;

            default:
                show_404();
                break;
        }
    }

    public function no_trash($id = false) {
        if ($id) {
            $falid_id = $this->packages->is_found(array('id' => $id, 'trash' => 'YES'), array(), 'row', $this->agent->referrer());
            $data['trash'] = 'NO';
            if ($this->packages->save($data, array('id' => $id, 'trash' => 'YES'), TRUE)) {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function delete_final($id = false) {
        if ($id) {
            $falid_id = $this->packages->is_found(array('id' => $id, 'trash' => 'YES'), array(), 'row', $this->agent->referrer());
            if ($this->packages->delete(array('id' => $id), FALSE)) {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function delete($id = FALSE) {
        if ($id) {
            $falid_id = $this->packages->is_found(array('id' => $id), array(), 'row', __CLASS__);
            if ($this->packages->delete(array('id' => $id))) {
                $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function trash($offset = false) {
        $limit = metohd_get_uri('dblimit', ADMIN_LIMIT);
        $count = $this->packages->filter(TRUE, FALSE, TRUE);
        $this->pass['count'] = $count;
        $this->pass['limit'] = $limit;
        $this->pass['result'] = $this->packages->filter(FALSE, $offset, TRUE);
        $this->pass['pagination'] = $this->admin_pagination('index', $count, $limit);
        $this->themes->render('packages/trash', $this->pass);
    }

}

?>
